<?php
/**
 * /depot/library/models/Module/I18n.php
 * @author qqinxl
 * 2008/03/28
 * 
 * @category  
 * @package  
 * @copyright  Copyright (c) {}
 */
class Module_Impl_I18n implements Module_Interface_Translate {  
   
	protected $_translator;
	
	protected $_errormsg;
	/**
	 * Singleton instance
	 * @var Module_Impl_I18n
	 */
	protected static $_instance = null;
	/**
	 * Singleton instance 
	 * @return Module_Impl_I18n
	 */
	public static function getInstance() {
		if (null === self::$_instance) {
			self::$_instance = new self ( );
		}
		return self::$_instance;
	}
	
   /**
   * Constructor
   * @return void
   */
	protected function __construct(){
		try{		
			if(empty($this->_translator)){
				$this->_translator = Zend_Registry::get("Zend_Translate");	
			}					
		}catch (Exception $e){
			throw $e;
		}
	}
	
	public function clearErrorMsg(){
		$this->_errormsg = null;
	}
	
	public function setErrorMsg($key,$value){
		$this->_errormsg[$key] = $value;
	}
	
	public function getErrorMsg(){
		return $this->_errormsg;
	}
	
	
	public function translateError($options = null){
		if(!empty($this->_errormsg)){
			foreach ($this->_errormsg as $key=>$value){
				$this->_errormsg[$key] = $this->_($value,$options);
			}			
		}
		return $this->getErrorMsg();
	}
	
	public function _($string,$options = null){
		$msg = $this->_translator->_($string);
		if(!empty($options) and is_array($options)){
			foreach ($options as $key=>$value){
				$msg = str_replace ( '{'.$key.'}', $value, $msg );
			}
		}
		return $msg;
	}		
}